package com.youbo.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

public class IDUtils {


    static SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmssSSS");




    public static void main(String[] ar){
//        for (int i=0;i<1000;i++){
//            System.out.println(getId8());
//        }

        String[] str="aaa_123".split("_");
        System.out.println(str[0]);
        System.out.println(str[1]);
    }

    /**
     * 以毫微秒做基础计数, 返回唯一有序增长ID
     * <pre>System.nanoTime()</pre>
     * <pre>
     *  线程数量:   100
     *  执行次数:   1000
     *  平均耗时:   222 ms
     *  数组长度:   100000
     *  Map Size:   100000
     * </pre>
     * @return  ID长度9位
     */
    public synchronized static String getPrimaryKey(){
        return MathUtils.makeUpNewData(Thread.currentThread().hashCode()+"", 2)+MathUtils.randomDigitNumber(6); //随机7位数
    }
    public synchronized static String getPrimaryKey8(){
        return MathUtils.makeUpNewData(Thread.currentThread().hashCode()+"", 2)+MathUtils.randomDigitNumber(5); //随机7位数
    }



    private static final long LIMIT = 10000000000L;
    private static long last = 0;

    public synchronized static String getID101() {
        long id = System.currentTimeMillis() % LIMIT;
        if ( id <= last ) {
            id = (last + 1) % LIMIT;
        }
        return  String.valueOf(id);
    }

    private static int inc = 0;
    public synchronized static String getID102(){
        long id = Long.parseLong(String.valueOf(System.currentTimeMillis())
                .substring(1,10)
                .concat(String.valueOf(inc)));
        inc = (inc+1)%10;
        return String.valueOf(id);
    }

    /**
     * 根据身份证号码 取年龄
     * @param sfz
     * @return
     */
    public static int getAge(String sfz){
        sfz = sfz.substring(6, sfz.length()-8);
        System.out.println(sfz);
        Calendar cal = Calendar.getInstance();
        java.util.Date date = cal.getTime();
        String s1 = date.toString();
        String s2 = (String) s1.subSequence(s1.length()-4, s1.length());
        System.out.println(date);
        System.out.println(s2);
        Integer int1 = Integer.parseInt(sfz);
        Integer int2 = Integer.parseInt(s2);
        return (int2-int1);
    }


    /**
     * 生成 18位订单号
     * @return
     */
    public synchronized static  String getUUId() {
        String newDate=sdf.format(new Date());
        String result="";
        int sjs=(int)(Math.random()*(99-10+1));
        String s=String.format("%02d",sjs);
        result+="1"+s;//2位随机数
        return (newDate+result);//
    }

    public static String getUUID32(){
        return UUID.randomUUID().toString().replace("-", "").toLowerCase();
    }

    /**
     * 生成 12号
     * @return
     */
    public static String getId12() {
        return UUID.randomUUID().toString().substring(24).toUpperCase();
    }

    /**
     * 生成 12号
     * @return
     */
    public static String getId8() {
        return UUID.randomUUID().toString().substring(24).toUpperCase();
    }





}
